<?php
class Order extends CI_Controller{
	private $per_page_index = 1;
	private $per_page_followup = 1;
	
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn() ) redirect("/");
		$this->load->model("model_transaction");
	}
	function index(){
		if( ($this->session->userdata("type_user") != "admin") && ($this->session->userdata("type_user") != "superadmin") ) redirect("/");
		$data['title'] = TITLE.' - Order';
		
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "admin_order.js";
		
		$params['order_by'] = "t.entry_date desc, t.transaction_id desc";
		
		$count = count($this->model_transaction->getTransaction($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/order/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = 0;
		$data['transaction_list'] = $this->model_transaction->getTransaction($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = "";
		$data['offset'] = 0;
		
		$this->load->view('admin/order',$data);
	}
	function load($offset=0){
		$key = trim($this->input->get("q",true));
		$keyword_decode = rawurldecode($key);
		$embedString = '';
		if($key != ""){
			$embedString = '?q='.rawurlencode($key);
		}
		$params['keyword'] = $keyword_decode;
		
		$order_by = "t.entry_date desc, t.transaction_id desc";
		
		$sort_by_field = $this->input->get("s",true);
		$sort_by_type = $this->input->get("t",true);
		
		if($this->input->get("s",true) ){
			$order_by = $sort_by_field . " " . $sort_by_type;
			$embedString = '?s='.$sort_by_field.'&t='.$sort_by_type;
		}
			
		$params['order_by'] = $order_by;
		$count = count($this->model_transaction->getTransaction($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/order/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		//manipulate pagination link to embed the suffix needed
		$data['pagination'] = Paging::embedSuffixInPaging($embedString,$data['pagination']);
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['transaction_list'] = $this->model_transaction->getTransaction($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = $embedString;
		$data['offset'] = $offset;
		
		$this->load->view('admin/content_load/order_load',$data);
	}
	
	function detail($trans_id){
		if( ($this->session->userdata("type_user") != "admin") && ($this->session->userdata("type_user") != "superadmin") ) redirect("/");
		$data['trans_id'] = $trans_id;
		$data['title'] = TITLE.' - Order Detail';
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "admin_order.js";
		$data['success'] = "";
		if($this->input->post("btnSaveResult") ){
			$hasil = $this->_saveFollowUpResult($trans_id);
			if($hasil) $data['success'] = "saved";
		}
		
		$detail = $this->model_transaction->getTransaction(array('order_detail'=>'y','transaction_id'=>$trans_id));
		$data['detail'] = $detail[0];
		$params = array(
				'transaction_id'=>$trans_id,
				'order_by' => 'entry_date desc, user_id asc',
			);
		$count = count($this->model_transaction->getFollowUpList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/order/load_followup/'.$trans_id;
		$config['uri_segment'] = 5;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_followup;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['limit_1'] = $this->per_page_followup;
		$params['limit_2'] = 0;
		$data['follow_up'] = $this->model_transaction->getFollowUpList($params);
		$data['per_page_followup'] = $this->per_page_followup;
		$data['embedString'] = "";
		
		$this->load->view('admin/order_detail',$data);
	}
	
	function load_followup($trans_id,$offset=0){
		$data['trans_id'] = $trans_id;
		$detail = $this->model_transaction->getTransaction(array('order_detail'=>'y','transaction_id'=>$trans_id));
		$data['detail'] = $detail[0];
		$params = array(
				'transaction_id'=>$trans_id,
				'order_by' => 'entry_date desc, user_id asc',
			);
		$count = count($this->model_transaction->getFollowUpList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/order/load_followup/'.$trans_id;
		$config['uri_segment'] = 5;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_followup;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['limit_1'] = $this->per_page_followup;
		$params['limit_2'] = $offset;
		$data['follow_up'] = $this->model_transaction->getFollowUpList($params);
		$data['per_page_followup'] = $this->per_page_followup;
		$data['embedString'] = "";
		
		$this->load->view('admin/content_load/followup_load',$data);
	}
	
	function _saveFollowUpResult($transaction_id){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'result_follow',
                     'label'   => 'Result',
                     'rules'   => 'trim|required|xss_clean'
                  )
				);
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_transaction->addFollowup($transaction_id);
			return true;
		}
	}
	
}